package com.aotain.nyx.netipstat;

import java.util.HashMap;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;

import com.aotain.common.CommonFunction;
import com.aotain.nyx.ipstat.IPStatTuple;
import com.aotain.nyx.statis.AbnStatisTuple;


/**
 * 
     *@Title:异常攻击类型统计 Map METIS(网络威胁平台)
     *@Description:
     *@Author:Turk
     *@Since:2017年8月31日
     *@Version:1.1.0
 */
public class AbnStatisMap implements MapFunction<AbnStatisTuple, Tuple2<String,IPStatTuple>> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2251740822545426679L;

	@Override
	public Tuple2<String, IPStatTuple> map(AbnStatisTuple value)
			throws Exception {
		// TODO Auto-generated method stub
		Tuple2<String,IPStatTuple> ret = new Tuple2<String,IPStatTuple>();
		if(value != null) {
			String dip = value.getDestIP();
			String sip = value.getSourceIP();
			String ip = sip;
			
			Long lDestIP = CommonFunction.ip2int(dip);
			Long lSourceIP = CommonFunction.ip2int(sip);
			
			//判断确定攻击IP
			String flowDirction = "IN";
			if(CommonFunction.IsLocalIP(lDestIP)) {
				flowDirction = "IN"; //受到的攻击
				ip = dip;
			}
			
			if(CommonFunction.IsLocalIP(lSourceIP)) {
				flowDirction = "OUT"; //发出的攻击
				ip = sip;
			}
			
			
			//
			
			IPStatTuple stat = new IPStatTuple();
			HashMap<String,Long> valueMap = new HashMap<String,Long>();			
			valueMap.put(value.getType(), value.getUpStreamOctets());
			
			//flowDirction + 
			valueMap.put(value.getType() + "NUM", value.getUpStreamPacket());
			valueMap.put(value.getType() + "ATTACKNUM", value.getAttackNum());
			valueMap.put(flowDirction + "ATTACKNUM", value.getAttackNum());//攻击次数，一次完整的攻击
			//stat.setDestIP(value.getDestIP());
			stat.setValueMap(valueMap);
			
			//异常总体统计
			valueMap.put("ATTOCT", value.getUpStreamOctets());
			valueMap.put("ATTPACKET", value.getUpStreamPacket());
			stat.setAttackMax(value.getAttackMax());
			ret.setFields(ip, stat);
		}
		return ret;
	}


}
